//
//  SurePaypwdController.m
//  WaywardPoint_Code
//
//  Created by 韩盼盼 on 15/9/29.
//  Copyright (c) 2015年 易网新科技. All rights reserved.
//

#import "SurePaypwdController.h"
#import "AFFNumericKeyboard.h"
#import "PasswordSetVC.h"//密码重置

#define PayCount 6

@interface SurePaypwdController ()<AFFNumericKeyboardDelegate,HeadBackView>
{
    UIButton *gobackBtn;
    UIView *sureView;
    UILabel *msgLab;
    NSDictionary *userdict;
}
@end

@implementation SurePaypwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.paypwdStr=@"";
    
    _surepaypwd=[NSMutableArray array];
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.view.tag=1;
    
    self.title=@"确认支付密码";
    
    UIButton *leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40*PROPORTION_WIDTH, 54*PROPORTION_HEIGHT)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"return_left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btnback) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftBarBtn;
        
    [self showUI];
}

- (void)showUI
{
    UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 65, ScreenW, ScreenH-65)];
    [self.view addSubview:contentView];
    
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake((ScreenW-150)*0.5, 10, 150, 25)];
    title.font=[UIFont systemFontOfSize:18];
    title.textColor = COLOT_TEXTGRAY;
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"请确认支付密码";
    [contentView addSubview:title];
    
    CGFloat btnH=50;
    
    //支付密码的框
    UIView *pwdview=[[UIView alloc]initWithFrame:CGRectMake(8, title.y+title.height+5, ScreenW-16, 50)];
    pwdview.layer.masksToBounds=YES;
    pwdview.layer.borderWidth=0.8;
    pwdview.layer.borderColor = [UIColor whiteColor].CGColor;
    pwdview.backgroundColor = COLOR_EVERYVIEWBG;
    [contentView addSubview:pwdview];
    CGFloat btnW=pwdview.width/6;
    msgLab=[[UILabel alloc]initWithFrame:CGRectMake(pwdview.x, pwdview.y+pwdview.height+5, pwdview.width, 10)];
    msgLab.hidden=YES;
    msgLab.textColor=RXDColor(223, 56, 109);
    msgLab.font=[UIFont boldSystemFontOfSize:12];
    [contentView addSubview:msgLab];
    for (int i=0; i<6; i++) {
        UITextField *pwdla=[[UITextField alloc]initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
        pwdla.textAlignment=NSTextAlignmentCenter;
        pwdla.secureTextEntry=YES;
        pwdla.textColor=[UIColor whiteColor];
        pwdla.enabled=NO;
        pwdla.keyboardType=UIKeyboardTypeNumberPad;
        pwdla.font=[UIFont systemFontOfSize:20];
        pwdla.layer.masksToBounds=YES;
        pwdla.layer.borderWidth=0.8;
        pwdla.layer.borderColor = [UIColor whiteColor].CGColor;

        pwdla.tag=i+300;
        [pwdview addSubview:pwdla];
    }
    
    UIView *keyboardview=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-214, ScreenW, 216)];
    keyboardview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:keyboardview];
    
    AFFNumericKeyboard *keyboard = [[AFFNumericKeyboard alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 216)];
    [keyboardview addSubview:keyboard];
    keyboard.delegate = self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    msgLab.hidden=YES;
    [self.surepaypwd removeAllObjects];
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    pwdT1.text=@"";
    pwdT2.text=@"";
    pwdT3.text=@"";
    pwdT4.text=@"";
    pwdT5.text=@"";
    pwdT6.text=@"";

}

- (void)btnback
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)numberKeyboardBackspace
{
    msgLab.hidden=YES;
    [self clearpaypwd];
}

-(void)numberKeyboardInput:(NSInteger)number
{
    if (self.surepaypwd.count< PayCount) {
        [self.surepaypwd addObject:[NSString stringWithFormat:@"%ld",(long)number]];
        [self getpaypwd:self.surepaypwd];
    }
}

-(NSArray *)substring:(NSString *)temp
{
    
    NSArray *array = [temp componentsSeparatedByString:@"^"]; //从字符A中分隔成2个元素的数组
    
    return array;
}
- (NSString *)getpaypwd:(NSArray *)pwdstrarr
{
    msgLab.hidden=YES;
    NSString *paypwdstr=@"";
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    
    if (pwdT1.text.length==0) {
        pwdT1.text=pwdstrarr[0];
    }else if (pwdT2.text.length==0)
    {
        pwdT2.text=pwdstrarr[1];
    }else if (pwdT3.text.length==0)
    {
        pwdT3.text=pwdstrarr[2];
    }else if (pwdT4.text.length==0)
    {
        pwdT4.text=pwdstrarr[3];
    }else if (pwdT5.text.length==0)
    {
        pwdT5.text=pwdstrarr[4];
    }else if (pwdT6.text.length==0)
    {
        pwdT6.text=pwdstrarr[5];
    }
    if (pwdT1.text.length!=0&&pwdT2.text.length!=0&&pwdT3.text.length!=0
        &&pwdT4.text.length!=0&&pwdT5.text.length!=0&&pwdT6.text.length!=0) {
        paypwdstr=[NSString stringWithFormat:@"%@%@%@%@%@%@",pwdT1.text,pwdT2.text,pwdT3.text,pwdT4.text,pwdT5.text,pwdT6.text];
        
        if ([self.tempstr isEqualToString:paypwdstr]) {
            _paypwdStr=paypwdstr;
            msgLab.hidden=YES;
            [self settingpaypwd];
        }
        else
        {
            msgLab.hidden=NO;
            msgLab.text=@"两次密码输入不一致";
            [self clearAllpaypwd];
        }
    }
    
    return  paypwdstr;
}

- (void)clearAllpaypwd
{

    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    pwdT1.text=@"";
    pwdT2.text=@"";
    pwdT3.text=@"";
    pwdT4.text=@"";
    pwdT5.text=@"";
    pwdT6.text=@"";
    [self.surepaypwd removeAllObjects];
    
}

- (void)popToView
{
    for (UIViewController *temp in self.navigationController.viewControllers) {
        
        if ([temp isKindOfClass:[PasswordSetVC class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }
}

- (void)clearpaypwd
{
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    if (pwdT6.text.length!=0) {
        pwdT6.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT5.text.length!=0)
    {
        pwdT5.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT4.text.length!=0)
    {
        pwdT4.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT3.text.length!=0)
    {
        pwdT3.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT2.text.length!=0)
    {
        pwdT2.text=@"";
        [self.surepaypwd removeLastObject];
    }else if (pwdT1.text.length!=0)
    {
        pwdT1.text=@"";
        [self.surepaypwd removeLastObject];
    }
}

#pragma mark - 设置支付密码
- (void)settingpaypwd
{
    //POST
    NSString *oldpwd=@"";
//    oldpwd=[MyMD5 md5:oldpwd];
    // 2.密码
//    NSString *pwdText = _paypwdStr;
//    NSLog(@"%@",_paypwdStr);
//    pwdText=[MyMD5 md5:pwdText];
    
    // 增加蒙板
    [MBProgressHUD showMessage:@"设置密码中..." toView:self.view ];
    // 创建一个URL ： 请求路径
    NSString *url = [NSString stringWithFormat:@"%@member/paypassword/update",MRAPI];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
    // 2.封装参数(这个字典只能放非文件参数)
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"memberid"] = [NSNumber numberWithInt:_memberid];
    params[@"oldpaypassword"] = oldpwd;
    params[@"newpaypassword"] = _paypwdStr;
    
       [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSString *error=responseObject[@"error"];
        
//        NSLog(@"setpassword%@---%@",responseObject,operation);
        
        if ([error isEqualToString:@""]) {
            userdict=[responseObject mutableCopy];
            [UIView showAlertMsg:@"设置成功"];
            
            [self saveUserinfo:userdict];
            
            Class cls=NSClassFromString(self.payjumpStr);
            for (UIViewController *temp in self.navigationController.viewControllers) {
                
                if ([temp isKindOfClass:[cls class]]) {
                    [self.navigationController popToViewController:temp animated:YES];
                }
            }
        }
        else
        {
            [UIView showAlertMsg:error];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"hahah%@",error);
    }];
}

/** 用户信息保存 */
- (void)saveUserinfo:(NSDictionary *)dict
{
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"issetpaypassword"];
    [[NSUserDefaults standardUserDefaults] synchronize];
   
}

@end
